home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.5 Complementary Applications 2004 February / SGI IRIX 6.5 Complementary Applications 2004 February.iso / dist / cde.idb / usr / dt / share / examples / dtsession / session.c.z / session.c
Encoding:
C/C++ Source or Header  |  2003-11-18  |  5.7 KB  |  239 lines

  1. /*
  2.  * session.c
  3.  *
  4.  * Copyright 2000, Silicon Graphics, Inc.
  5.  * ALL RIGHTS RESERVED
  6.  * 
  7.  * UNPUBLISHED -- Rights reserved under the copyright laws of the United
  8.  * States.   Use of a copyright notice is precautionary only and does not
  9.  * imply publication or disclosure.
  10.  *
  11.  * U.S. GOVERNMENT RESTRICTED RIGHTS LEGEND:
  12.  * Use, duplication or disclosure by the Government is subject to restrictions
  13.  * as set forth in FAR 52.227.19(c)(2) or subparagraph (c)(1)(ii) of the Rights
  14.  * in Technical Data and Computer Software clause at DFARS 252.227-7013 and/or
  15.  * in similar or successor clauses in the FAR, or the DOD or NASA FAR
  16.  * Supplement.  Contractor/manufacturer is Silicon Graphics, Inc.,
  17.  * 2011 N. Shoreline Blvd. Mountain View, CA 94039-7311.
  18.  *
  19.  * THE CONTENT OF THIS WORK CONTAINS CONFIDENTIAL AND PROPRIETARY
  20.  * INFORMATION OF SILICON GRAPHICS, INC. ANY DUPLICATION, MODIFICATION,
  21.  * DISTRIBUTION, OR DISCLOSURE IN ANY FORM, IN WHOLE, OR IN PART, IS STRICTLY
  22.  * PROHIBITED WITHOUT THE PRIOR EXPRESS WRITTEN PERMISSION OF SILICON
  23.  * GRAPHICS, INC.
  24.  */
  25. /* $XConsortium: session.c /main/cde1_maint/1 1995/07/17 16:44:52 drk $ */
  26. /*
  27.  * (c) Copyright 1993, 1994 Hewlett-Packard Company    
  28.  * (c) Copyright 1993, 1994 International Business Machines Corp.
  29.  * (c) Copyright 1993, 1994 Sun Microsystems, Inc.
  30.  * (c) Copyright 1993, 1994 Novell, Inc.
  31.  */
  32.  
  33.  
  34. /*
  35.  * session.c
  36.  *
  37.  * Example code for Dt Session Manager conventions and API
  38.  */
  39.  
  40. #include <stdio.h>
  41. #include <Xm/RowColumn.h>
  42. #include <Xm/ToggleB.h>
  43. #include <Xm/Protocols.h>
  44. #include <Dt/Session.h>
  45.  
  46.  
  47. /*
  48.  * Define '-session' command line option
  49.  */
  50.  
  51. typedef struct _ApplicationArgs {
  52.     String session;
  53. } ApplicationArgs;
  54.  
  55. static XtResource applicationResources[] = {
  56.     {
  57.     "session",
  58.     "Session",
  59.     XmRString,
  60.     sizeof(String),
  61.     XtOffsetOf(ApplicationArgs,session),
  62.     XmRImmediate,
  63.     NULL
  64.     },
  65. };
  66.  
  67. static XrmOptionDescRec commandLineOpts[] = {
  68.     { "-session", "session", XrmoptionSepArg, NULL },
  69. };
  70.  
  71. static ApplicationArgs applicationArgs;
  72.  
  73. /*
  74.  * Simple application state to be preserved across sessions
  75.  */
  76.  
  77. static int lightState = False;
  78.  
  79. /*
  80.  * miscellaneous global data
  81.  */
  82.  
  83. static XtAppContext appContext;
  84. static Widget toplevel;
  85. static int savedArgc;
  86. static char **savedArgv;
  87.  
  88. static void PreserveCommandLine(int, char **);
  89. static void SetWmCommand(char *);
  90. static void SaveSessionCb(Widget, XtPointer, XtPointer);
  91. static void RestoreSession(Widget, char*);
  92. static void SaveApplicationState(char *);
  93. static void RestoreApplicationState(char *);
  94.  
  95.  
  96. main(int argc, char **argv)
  97. {
  98.     Widget mainWindow, toggle;
  99.     XmString labelString;
  100.     Arg args[1];
  101.     
  102.     /* Save the command line before Xt parses out the standard options */
  103.     PreserveCommandLine(argc, argv);
  104.  
  105.     /* Create the application UI */
  106.  
  107.     toplevel = XtAppInitialize(&appContext, "Session",
  108.                 commandLineOpts, XtNumber(commandLineOpts),
  109.                 &argc, argv,
  110.                 NULL,
  111.                 NULL, 0);
  112.  
  113.     XtGetApplicationResources(toplevel, &applicationArgs,
  114.                 applicationResources,
  115.                 XtNumber(applicationResources),
  116.                 NULL, 0);
  117.  
  118.     mainWindow = XmCreateWorkArea(toplevel, "mainWindow", NULL, 0);
  119.     XtManageChild(mainWindow);
  120.  
  121.     labelString = XmStringCreateLocalized("Lights");
  122.     XtSetArg(args[0], XmNlabelString, labelString);
  123.     toggle = XmCreateToggleButton(mainWindow, "lightsToggle", args, 1);
  124.     XtManageChild(toggle);
  125.     XmStringFree(labelString);
  126.  
  127.     /* Add callback to detect session manager messages */
  128.  
  129.     XmAddWMProtocolCallback(toplevel,
  130.         XInternAtom(XtDisplay(toplevel), "WM_SAVE_YOURSELF", False),
  131.         SaveSessionCb, (XtPointer)toplevel);
  132.  
  133.     /* Restore state if application was restarted by session manager */
  134.  
  135.     if (applicationArgs.session != NULL) {
  136.     RestoreSession(toplevel, applicationArgs.session);
  137.     }
  138.  
  139.     XtRealizeWidget(toplevel);
  140.     XtAppMainLoop(appContext);
  141. }
  142.  
  143.  
  144. /*
  145.  * Save session state
  146.  */
  147.  
  148. static void SaveSessionCb(Widget w, XtPointer cd, XtPointer cb)
  149. {
  150.     Widget toplevel = (Widget)cd;
  151.     char *savePath = NULL;
  152.     char *saveFile = NULL;
  153.  
  154.     DtSessionSavePath(toplevel, &savePath, &saveFile);
  155.  
  156.     if (savePath != NULL) {
  157.     SaveApplicationState(savePath);
  158.     SetWmCommand(saveFile);
  159.     XtFree(savePath);
  160.     XtFree(saveFile);
  161.     }
  162. }
  163.  
  164. static void SaveApplicationState(char *path)
  165. {
  166.     Widget toggle = XtNameToWidget(toplevel, "*lightsToggle");
  167.     FILE *fp;
  168.  
  169.     lightState = XmToggleButtonGetState(toggle);
  170.  
  171.     if ((fp = fopen(path, "w")) != NULL) {
  172.     fprintf(fp, "%d", lightState);
  173.     fclose(fp);
  174.     }
  175. }
  176.  
  177. static void PreserveCommandLine(int argc, char **argv)
  178. {
  179.     int i;
  180.  
  181.     savedArgv = (char **)XtMalloc(argc*sizeof(char *));
  182.     savedArgc = argc;
  183.     for (i=0; i < argc; i++) savedArgv[i] = XtNewString(argv[i]);
  184. }
  185.  
  186.  
  187. static void SetWmCommand(char *sessionId)
  188. {
  189.     char **wm_command;
  190.     int i, j;
  191.  
  192.     wm_command = (char **) XtMalloc((savedArgc+2) * sizeof(char*));
  193.     wm_command[0] = XtNewString(savedArgv[0]);
  194.     wm_command[1] = XtNewString("-session");
  195.     wm_command[2] = XtNewString(sessionId);
  196.  
  197.     for (i = 1, j = 3; i < savedArgc; i++) {
  198.         if (strcmp(savedArgv[i], "-session") == 0) { i++; continue; }
  199.         wm_command[j] = XtNewString(savedArgv[i]);
  200.         j++;
  201.     }
  202.  
  203.     XSetCommand(XtDisplay(toplevel), XtWindow(toplevel), wm_command, j);
  204.  
  205.     for (i=0; i < j; i++) XtFree(wm_command[i]);
  206.     XtFree((char*)wm_command);
  207. }
  208.  
  209. /*
  210.  * Restore previously saved state
  211.  */
  212.  
  213. static void RestoreSession(Widget w, char *restoreFile)
  214. {
  215.     char *restorePath = NULL;
  216.  
  217.     DtSessionRestorePath(w, &restorePath, restoreFile);
  218.  
  219.     if (restorePath != NULL) {
  220.     RestoreApplicationState(restorePath);
  221.     XtFree(restorePath);
  222.     }
  223. }
  224.  
  225. static void RestoreApplicationState(char *path)
  226. {
  227.     Widget toggle = XtNameToWidget(toplevel, "*lightsToggle");
  228.     FILE *fp;
  229.  
  230.     if ((fp = fopen(path, "r")) != NULL) {
  231.     fscanf(fp, "%d", &lightState);
  232.     fclose(fp);
  233.     }
  234.  
  235.     XmToggleButtonSetState(toggle, lightState, False);
  236. }
  237.  
  238.  
  239.